#ifndef THREAD_FIXED_POINT_H
#define THREAD_FIXED_POINT_H
//在表中，x和y是fixed数字，n是整数，定点数字是有符号的`p.q`格式，其中p+q=31，f是1<<q。
typedef int fixed_t;
//Convert `n` to fixed point:
#define fp_conv(n) ((fixed_t)(n << FP_Q))
#define FP_Q 15

//Add `x` and `y`:
#define fp_add(x, y) ((x + y))
//Add `x` and `n`:
#define fp_int_add(x, n) (x + (n << FP_Q))

//Subtract `y` from `x`:
#define fp_sub(x, y) (x - y)
//Subtract `n` from `x`:
#define fp_int_sub(x, n) (x - (n << FP_Q))

//Multiply `x` by `n`:
#define fp_int_mult(x, n) (x * n)

//Divide `x` by `n`:
#define fp_int_div(x, n) (x / n)

//Multiply `x` by `y`:
#define fp_mult(x, y) ((fixed_t)(((int64_t)x) * y >> FP_Q))
//Divide `x` by `y`:
#define fp_div(x, y) ((fixed_t)((((int64_t)x) << FP_Q) / y))
//Convert `x` to integer (rounding toward zero):
#define fp_getInt(x) (x >> FP_Q)
//Convert `x` to integer (rounding to nearest):
#define fp_rounded(x) (x >= 0 ? ((x + (1 << (FP_Q - 1))) >> FP_Q) \
                              : ((x - (1 << (FP_Q - 1))) >> FP_Q))
#endif